package com.nlelpct.security.model;

import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.*;

/**
 * @author :小马
 * @date : 2023-07-12
 */
@Data
public class MyUser implements UserDetails {

    /**
     * 当前登录用户ID
     */
    private String userId;

    /**
     * 当前登录用户名 -- 手机号
     */
    private String username;

    /**
     * 密码
     */
    private String password;

    /**
     * 用户是否被禁止的标识
     */
    private Boolean enabled;

    /**
     * 存储当前用户的所有角色权限字符串
     */
    private List<String> roles = new ArrayList<>();

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        Set<SimpleGrantedAuthority> authorities = new HashSet<>();
        roles.forEach(role -> {
            authorities.add(new SimpleGrantedAuthority(role));
        });
        return authorities;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return this.enabled;
    }
}
